﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Fonqi.ColorPicker;

namespace Fonqi.ColorPicker
{
    public partial class DataEditor : System.Web.UI.UserControl, 
        umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FireCustomScript(this, Resource.jscolor);
        }

        public object value
        {
            get
            {
                return this.Color.Text;
            }
            set
            {
                Setup();

                if (value != null)
                {
                    this.Color.Text = value.ToString();
                }
            }
        }

        private void Setup()
        {
            string folder = "inasesfclmntnes";
            if (!Directory.Exists(Server.MapPath(folder)))
                Directory.CreateDirectory(Server.MapPath(folder));
            if (!File.Exists(Server.MapPath(folder + "/hs.png")))                
                Resource.hs.Save(Server.MapPath(folder + "/hs.png"));
            if (!File.Exists(Server.MapPath(folder + "/hv.png")))
                Resource.hv.Save(Server.MapPath(folder + "/hv.png"));
            if (!File.Exists(Server.MapPath(folder + "/arrow.png")))
                Resource.arrow.Save(Server.MapPath(folder + "/arrow.gif"));
            if (!File.Exists(Server.MapPath(folder + "/cross.png")))
                Resource.cross.Save(Server.MapPath(folder + "/cross.gif"));

            FireCustomScript(this, Resource.jscolor);
        }

        public static void FireCustomScript(Control control, string script)
        {
            control.Page.ClientScript.RegisterStartupScript(control.GetType(), "CustomScript", EmbedScript(script));
        }

        public static string EmbedScript(string script)
        {
            return "<script type='text/javascript'>\n" + script + "\n</script>";
        }
    }
}